package no.group.gowther.rest;

import no.group.gowther.enums.OnePieceType;
import no.group.gowther.rest.vo.OnePieceDetailVO;
import no.group.gowther.rest.vo.OnePieceListVO;
import no.group.gowther.service.dto.*;
import no.group.gowther.service.dto.param.OnePieceQueryDTO;
import no.group.gowther.service.dto.param.OnePieceSaveDTO;
import no.group.gowther.service.dto.param.OnePieceUpdateDTO;
import no.group.gowther.rest.vo.param.OnePieceCreateParam;
import no.group.gowther.rest.vo.param.OnePieceQueryParam;
import no.group.gowther.rest.vo.param.OnePieceUpdateParam;
import no.group.gowther.rest.vo.Result;
import no.group.gowther.service.OnePieceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ayatsuji
 * @date 2022-04-24 22:23
 * @since
 */
@Validated
@RestController
@RequestMapping("api/one-piece")
public class OnePieceRestController {
    private final OnePieceService onePieceService;

    public OnePieceRestController(OnePieceService onePieceService) {
        this.onePieceService = onePieceService;
    }

    @PostMapping
    public Result<?> filing(@Validated @RequestBody OnePieceCreateParam onePieceCreateParam) {
        OnePieceSaveDTO saveDTO = new OnePieceSaveDTO(Paths.get(onePieceCreateParam.getFilePath()), onePieceCreateParam.getName(), onePieceCreateParam.getTagId());
        return Result.success(onePieceService.create(saveDTO));
    }

    @PutMapping
    public Result<?> update(@Validated @RequestBody OnePieceUpdateParam onePieceUpdateParam) {
        OnePieceUpdateDTO onePieceUpdateDTO = new OnePieceUpdateDTO();
        onePieceUpdateDTO.setId(onePieceUpdateParam.getOnePieceId());
        onePieceUpdateDTO.setTagId(onePieceUpdateParam.getTagId());
        onePieceUpdateDTO.setName(onePieceUpdateParam.getName());
        onePieceService.update(onePieceUpdateDTO);
        return Result.success();
    }

    @GetMapping
    public Result<ListDTO<OnePieceListVO>> queryList(OnePieceQueryParam onePieceParam) {
        OnePieceQueryDTO onePieceQueryDTO = new OnePieceQueryDTO(onePieceParam);
        final List<OnePieceType> onePieceTypes = onePieceParam.getType().stream().map(OnePieceType::valueOf).collect(Collectors.toList());
        onePieceQueryDTO.setType(onePieceTypes);
        onePieceQueryDTO.setTagId(onePieceParam.getTagId());
        ListDTO<OnePieceListDTO> dtoList = onePieceService.queryList(onePieceQueryDTO);
        ListDTO<OnePieceListVO> voList = new ListDTO<>(dtoList.total(), dtoList.list().stream().map(OnePieceListVO::new).collect(Collectors.toList()));
        return Result.success(voList);
    }

    @GetMapping("{id}")
    public Result<OnePieceDetailVO> queryDetail(@PathVariable @NotNull(message = "id不能为空") Long id) {
        return Result.success(new OnePieceDetailVO(onePieceService.queryDetail(id)));
    }
}
